const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: true, //显示筛选日期弹出层
    shopId: "",//门店id
    cardNo:"",//卡号
    expenseData: [], //消费记录
    isHints: false, //暂无数据
    month: 1, //查询几个月的明细
    monthTitle: "一个月", //月份标题
    topImg: "https://oss.kiscloud.net/wechat/images/icon-arrow-top-square.png",
    downImg: "https://oss.kiscloud.net/wechat/images/icon-arrow-down-square.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      shopId: options.shopId,
      cardNo: options.cardNo ? options.cardNo:""
    }, function() {
      this.getCoinMonthDetail();
    })
  },

  /**
   * 根据菜单查询
   */
  selectMonthDetails(event) {
    var id = event.currentTarget.dataset.id; //月数
    var title = event.currentTarget.dataset.title; //月标题
    this.setData({
      month: id,
      monthTitle: title,
      showView: true,
      expenseData: [] //数据集
    }, function() {
      this.getCoinMonthDetail();
    })
  },
  /**
   * 消费记录月资产
   */
  getCoinMonthDetail() {
    var param = {};
    param.merchantId=wx.getStorageSync('merchantId');
    param.shopId = this.data.shopId; //门店id
    param.cardNo = this.data.cardNo;//卡号
    param.month = this.data.month; //查询几个月的明细
    param.offset = 0;
    param.limit = 500;
    memberModel.coinMonthDetail(param).then(data => {
      if (data && data.data && data.totalCount > 0) {
        var itemData = [];
        for (var i = 0; i < data.data.length; i++) {
          var list = {};
          var item = data.data[i];
          list.day = item.dateline || ""; //时间 
          list.dateline = item.dateline ? item.dateline.replace(/-/g, "/") : "" || ""; //时间
          list.expense = item.expense > 0 ? "-" + item.expense : item.expense || 0; //消费币数
          list.income = item.income > 0 ? "+" + item.income : item.income || 0; //收入币数
          list.toggle = false;
          list.dayData = []; //天详情
          itemData.push(list);
        }
        this.setData({
          isHints: false,
          expenseData: itemData
        })
      } else {
        this.setData({
          isHints: true,
          expenseData: []
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 消费记录日资产
   */
  getCoinDayDetail(event) {
    var index = event.currentTarget.dataset.index; //门店id
    var date = event.currentTarget.dataset.time; //月数
    var nowToggle = event.currentTarget.dataset.toggle;
    var param = {};
    param.merchantId=wx.getStorageSync('merchantId');
    param.shopId = this.data.shopId; //门店id
    param.cardNo = this.data.cardNo;//卡号
    param.date = date; //日期
    param.offset = 0;
    param.limit = 2000;
    memberModel.coinDayDetail(param).then(data => {
      if (data && data.data && data.totalCount > 0) {
        var itemDay=[];
        for (var j = 0; j < data.data.length; j++) {
          var item=data.data[j];
          var list={};
          list.time = item.time ? item.time.substr(11,8):"" || "";//时间
          list.operation = item.operation || "";//操作类型
          list.coin = item.coin || "";//游戏币数量
          itemDay.push(list);
        }
        this.setData({
          ['expenseData[' + index + '].dayData']: itemDay,
          ['expenseData[' + index + '].toggle']: !nowToggle
        });
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   *选择查询时间 
   */
  selectDate: function() {
    this.setData({
      showView: (!this.data.showView)
    })
  },

})